using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

using WeifenLuo.WinFormsUI;
using rpaulo.toolbar;
using AForge.Imaging;

	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form, IDocumentsHost
	{	
        private int unnamedNumber = 0;
		private Configuration config = new Configuration();
		private HistogramWindow histogramWin = new HistogramWindow();

		private ToolBarManager toolBarManager;
		private WeifenLuo.WinFormsUI.DockManager dockManager;
		private System.Windows.Forms.MainMenu mainMenu;
		private System.Windows.Forms.MenuItem fileItem;
		private System.Windows.Forms.MenuItem exitFileItem;
		private System.Windows.Forms.MenuItem OpenItem;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem closeFileItem;
		private System.Windows.Forms.MenuItem closeAllFileItem;
		private System.Windows.Forms.MenuItem optionsItem;
		private System.Windows.Forms.MenuItem openInNewOptionsItem;
		private System.Windows.Forms.MenuItem viewItem;
        private System.Windows.Forms.MenuItem windowItem;
		private System.Windows.Forms.MenuItem histogramViewItem;
		private System.Windows.Forms.MenuItem redHistogramViewItem;
		private System.Windows.Forms.MenuItem greenHistogramViewItem;
		private System.Windows.Forms.MenuItem blueHistogramViewItem;
		private System.Windows.Forms.StatusBarPanel zoomPanel;
		private System.Windows.Forms.StatusBarPanel sizePanel;
		private System.Windows.Forms.StatusBarPanel infoPanel;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.StatusBar statusBar;
		private System.Windows.Forms.MenuItem rememberOptionsItem;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem reloadFileItem;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem centerViewItem;
		private System.Windows.Forms.StatusBarPanel selectionPanel;
		private System.Windows.Forms.OpenFileDialog ofd;
		private System.Windows.Forms.StatusBarPanel colorPanel;
		private System.Windows.Forms.ImageList imageList;
		private System.Windows.Forms.ToolBar mainToolBar;
		private System.Windows.Forms.ToolBarButton openButton;
		private System.Windows.Forms.ToolBarButton sep1;
		private System.Windows.Forms.ToolBarButton histogramButton;
		private System.Windows.Forms.ToolBar imageToolBar;
		private System.Windows.Forms.ImageList imageList2;
		private System.Windows.Forms.ToolBarButton cloneButton;
		private System.Windows.Forms.ToolBarButton cropButton;
		private System.Windows.Forms.ToolBarButton toolBarButton1;
		private System.Windows.Forms.ToolBarButton toolBarButton2;
		private System.Windows.Forms.ToolBarButton zoomInButton;
		private System.Windows.Forms.ToolBarButton zoomOutButton;
		private System.Windows.Forms.ToolBarButton toolBarButton3;
        private System.Windows.Forms.ToolBarButton fitToScreenButton;
		private System.Windows.Forms.ToolBarButton toolBarButton5;
		private System.Windows.Forms.ToolBarButton levelsButton;
		private System.Windows.Forms.ToolBarButton grayscaleButton;
		private System.Windows.Forms.ToolBarButton thresholdButton;
		private System.Windows.Forms.ToolBarButton toolBarButton6;
		private System.Windows.Forms.ToolBarButton morphologyButton;
		private System.Windows.Forms.ToolBarButton convolutionButton;
		private System.Windows.Forms.MenuItem mainBarViewItem;
		private System.Windows.Forms.MenuItem imageBarViewItem;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.ToolBarButton resizeButton;
		private System.Windows.Forms.ToolBarButton toolBarButton7;
		private System.Windows.Forms.ToolBarButton rotateButton;
		private System.Windows.Forms.StatusBarPanel hslPanel;
		private System.Windows.Forms.ToolBarButton toolBarButton8;
		private System.Windows.Forms.ToolBarButton saturationButton;
		private System.Windows.Forms.ToolBarButton fourierButton;
		private System.Windows.Forms.MenuItem copyFileItem;
		private System.Windows.Forms.MenuItem pasteFileItem;
		private System.Windows.Forms.MenuItem menuItem5;
        private System.Windows.Forms.MenuItem saveFileItem;
		private System.Windows.Forms.SaveFileDialog sfd;
		private System.Windows.Forms.ToolBarButton pasteButton;
		private System.Windows.Forms.ToolBarButton saveButton;
		private System.Windows.Forms.ToolBarButton copyButton;
		private System.Windows.Forms.ToolBarButton toolBarButton9;
		private System.Drawing.Printing.PrintDocument printDocument;
		private System.Windows.Forms.PrintPreviewDialog printPreviewDialog;
		private System.Windows.Forms.MenuItem printPreviewFileItem;
		private System.Windows.Forms.MenuItem pageSetupFileItem;
		private System.Windows.Forms.MenuItem printFileItem;
		private System.Windows.Forms.MenuItem menuItem8;
		private System.Windows.Forms.PageSetupDialog pageSetupDialog;
		private System.Windows.Forms.PrintDialog printDialog;
        //private StatusBarPanel ToggleMenu;
		private System.ComponentModel.IContainer components;

		public MainForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
            //
			// TODO: Add any constructor code after InitializeComponent call
			//
            toolBarManager = new ToolBarManager(this, this);

			// add toolbars
			ToolBarDockHolder holder;

			// main tool bar
			mainToolBar.Text = "Main Tool Bar";
			holder = toolBarManager.AddControl(mainToolBar,DockStyle.Left);
			holder.AllowedBorders = AllowedBorders.Top | AllowedBorders.Left | AllowedBorders.Right;
            
			// image toolbar
			imageToolBar.Text = "Image Tool Bar";
			holder = toolBarManager.AddControl(imageToolBar,DockStyle.Right);
			holder.AllowedBorders = AllowedBorders.Top | AllowedBorders.Left | AllowedBorders.Right;

            imageToolBar.Text = "Status Bar";
            
            
			histogramWin.DockStateChanged += new EventHandler(histogram_DockStateChanged);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
            this.mainMenu = new System.Windows.Forms.MainMenu(this.components);
            this.fileItem = new System.Windows.Forms.MenuItem();
            this.OpenItem = new System.Windows.Forms.MenuItem();
            this.reloadFileItem = new System.Windows.Forms.MenuItem();
            this.saveFileItem = new System.Windows.Forms.MenuItem();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.copyFileItem = new System.Windows.Forms.MenuItem();
            this.pasteFileItem = new System.Windows.Forms.MenuItem();
            this.menuItem5 = new System.Windows.Forms.MenuItem();
            this.closeFileItem = new System.Windows.Forms.MenuItem();
            this.closeAllFileItem = new System.Windows.Forms.MenuItem();
            this.menuItem8 = new System.Windows.Forms.MenuItem();
            this.pageSetupFileItem = new System.Windows.Forms.MenuItem();
            this.printFileItem = new System.Windows.Forms.MenuItem();
            this.printPreviewFileItem = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.exitFileItem = new System.Windows.Forms.MenuItem();
            this.viewItem = new System.Windows.Forms.MenuItem();
            this.mainBarViewItem = new System.Windows.Forms.MenuItem();
            this.imageBarViewItem = new System.Windows.Forms.MenuItem();
            this.menuItem7 = new System.Windows.Forms.MenuItem();
            this.histogramViewItem = new System.Windows.Forms.MenuItem();
            this.redHistogramViewItem = new System.Windows.Forms.MenuItem();
            this.greenHistogramViewItem = new System.Windows.Forms.MenuItem();
            this.blueHistogramViewItem = new System.Windows.Forms.MenuItem();
            this.menuItem3 = new System.Windows.Forms.MenuItem();
            this.centerViewItem = new System.Windows.Forms.MenuItem();
            this.optionsItem = new System.Windows.Forms.MenuItem();
            this.openInNewOptionsItem = new System.Windows.Forms.MenuItem();
            this.rememberOptionsItem = new System.Windows.Forms.MenuItem();
            this.windowItem = new System.Windows.Forms.MenuItem();
            this.statusBar = new System.Windows.Forms.StatusBar();
            this.zoomPanel = new System.Windows.Forms.StatusBarPanel();
            this.sizePanel = new System.Windows.Forms.StatusBarPanel();
            this.selectionPanel = new System.Windows.Forms.StatusBarPanel();
            this.colorPanel = new System.Windows.Forms.StatusBarPanel();
            this.hslPanel = new System.Windows.Forms.StatusBarPanel();
            this.infoPanel = new System.Windows.Forms.StatusBarPanel();
            this.panel1 = new System.Windows.Forms.Panel();
            this.dockManager = new WeifenLuo.WinFormsUI.DockManager();
            this.mainToolBar = new System.Windows.Forms.ToolBar();
            this.openButton = new System.Windows.Forms.ToolBarButton();
            this.saveButton = new System.Windows.Forms.ToolBarButton();
            this.sep1 = new System.Windows.Forms.ToolBarButton();
            this.copyButton = new System.Windows.Forms.ToolBarButton();
            this.pasteButton = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton9 = new System.Windows.Forms.ToolBarButton();
            this.histogramButton = new System.Windows.Forms.ToolBarButton();
            this.imageList = new System.Windows.Forms.ImageList(this.components);
            this.imageToolBar = new System.Windows.Forms.ToolBar();
            this.cloneButton = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
            this.cropButton = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
            this.zoomInButton = new System.Windows.Forms.ToolBarButton();
            this.zoomOutButton = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
            this.fitToScreenButton = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton5 = new System.Windows.Forms.ToolBarButton();
            this.resizeButton = new System.Windows.Forms.ToolBarButton();
            this.rotateButton = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton7 = new System.Windows.Forms.ToolBarButton();
            this.levelsButton = new System.Windows.Forms.ToolBarButton();
            this.grayscaleButton = new System.Windows.Forms.ToolBarButton();
            this.thresholdButton = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton6 = new System.Windows.Forms.ToolBarButton();
            this.morphologyButton = new System.Windows.Forms.ToolBarButton();
            this.convolutionButton = new System.Windows.Forms.ToolBarButton();
            this.toolBarButton8 = new System.Windows.Forms.ToolBarButton();
            this.saturationButton = new System.Windows.Forms.ToolBarButton();
            this.fourierButton = new System.Windows.Forms.ToolBarButton();
            this.imageList2 = new System.Windows.Forms.ImageList(this.components);
            this.ofd = new System.Windows.Forms.OpenFileDialog();
            this.sfd = new System.Windows.Forms.SaveFileDialog();
            this.printDocument = new System.Drawing.Printing.PrintDocument();
            this.printPreviewDialog = new System.Windows.Forms.PrintPreviewDialog();
            this.pageSetupDialog = new System.Windows.Forms.PageSetupDialog();
            this.printDialog = new System.Windows.Forms.PrintDialog();
            ((System.ComponentModel.ISupportInitialize)(this.zoomPanel)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.sizePanel)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.selectionPanel)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.colorPanel)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.hslPanel)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.infoPanel)).BeginInit();
            this.panel1.SuspendLayout();
            this.dockManager.SuspendLayout();
            this.SuspendLayout();
            // 
            // mainMenu
            // 
            this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.fileItem,
            this.viewItem,
            this.optionsItem,
            this.windowItem});
            // 
            // fileItem
            // 
            this.fileItem.Index = 0;
            this.fileItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.OpenItem,
            this.reloadFileItem,
            this.saveFileItem,
            this.menuItem1,
            this.copyFileItem,
            this.pasteFileItem,
            this.menuItem5,
            this.closeFileItem,
            this.closeAllFileItem,
            this.menuItem8,
            this.pageSetupFileItem,
            this.printFileItem,
            this.printPreviewFileItem,
            this.menuItem2,
            this.exitFileItem});
            this.fileItem.Text = "&File";
            this.fileItem.Visible = false;
            this.fileItem.Popup += new System.EventHandler(this.fileItem_Popup);
            // 
            // OpenItem
            // 
            this.OpenItem.Enabled = false;
            this.OpenItem.Index = 0;
            this.OpenItem.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
            this.OpenItem.Text = "&Open";
            this.OpenItem.Click += new System.EventHandler(this.OpenItem_Click);
            // 
            // reloadFileItem
            // 
            this.reloadFileItem.Enabled = false;
            this.reloadFileItem.Index = 1;
            this.reloadFileItem.Shortcut = System.Windows.Forms.Shortcut.CtrlR;
            this.reloadFileItem.Text = "&Reload";
            this.reloadFileItem.Click += new System.EventHandler(this.reloadFileItem_Click);
            // 
            // saveFileItem
            // 
            this.saveFileItem.Index = 2;
            this.saveFileItem.Shortcut = System.Windows.Forms.Shortcut.CtrlS;
            this.saveFileItem.Text = "&Save";
            this.saveFileItem.Click += new System.EventHandler(this.saveFileItem_Click);
            // 
            // menuItem1
            // 
            this.menuItem1.Index = 3;
            this.menuItem1.Text = "-";
            // 
            // copyFileItem
            // 
            this.copyFileItem.Index = 4;
            this.copyFileItem.Shortcut = System.Windows.Forms.Shortcut.CtrlC;
            this.copyFileItem.Text = "&Copy";
            this.copyFileItem.Click += new System.EventHandler(this.copyFileItem_Click);
            // 
            // pasteFileItem
            // 
            this.pasteFileItem.Index = 5;
            this.pasteFileItem.Shortcut = System.Windows.Forms.Shortcut.CtrlV;
            this.pasteFileItem.Text = "&Paste";
            this.pasteFileItem.Click += new System.EventHandler(this.pasteFileItem_Click);
            // 
            // menuItem5
            // 
            this.menuItem5.Index = 6;
            this.menuItem5.Text = "-";
            // 
            // closeFileItem
            // 
            this.closeFileItem.Enabled = false;
            this.closeFileItem.Index = 7;
            this.closeFileItem.Shortcut = System.Windows.Forms.Shortcut.CtrlF4;
            this.closeFileItem.Text = "C&lose";
            this.closeFileItem.Click += new System.EventHandler(this.closeFileItem_Click);
            // 
            // closeAllFileItem
            // 
            this.closeAllFileItem.Enabled = false;
            this.closeAllFileItem.Index = 8;
            this.closeAllFileItem.Text = "Close All";
            this.closeAllFileItem.Click += new System.EventHandler(this.closeAllFileItem_Click);
            // 
            // menuItem8
            // 
            this.menuItem8.Index = 9;
            this.menuItem8.Text = "-";
            // 
            // pageSetupFileItem
            // 
            this.pageSetupFileItem.Index = 10;
            this.pageSetupFileItem.Text = "Page Setup";
            this.pageSetupFileItem.Click += new System.EventHandler(this.pageSetupFileItem_Click);
            // 
            // printFileItem
            // 
            this.printFileItem.Index = 11;
            this.printFileItem.Text = "&Print";
            this.printFileItem.Click += new System.EventHandler(this.printFileItem_Click);
            // 
            // printPreviewFileItem
            // 
            this.printPreviewFileItem.Index = 12;
            this.printPreviewFileItem.Text = "Print Preview";
            this.printPreviewFileItem.Click += new System.EventHandler(this.printPreviewFileItem_Click);
            // 
            // menuItem2
            // 
            this.menuItem2.Index = 13;
            this.menuItem2.Text = "-";
            // 
            // exitFileItem
            // 
            this.exitFileItem.Enabled = false;
            this.exitFileItem.Index = 14;
            this.exitFileItem.Text = "E&xit";
            this.exitFileItem.Click += new System.EventHandler(this.exitFileItem_Click);
            // 
            // viewItem
            // 
            this.viewItem.Index = 1;
            this.viewItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.mainBarViewItem,
            this.imageBarViewItem,
            this.menuItem7,
            this.histogramViewItem,
            this.redHistogramViewItem,
            this.greenHistogramViewItem,
            this.blueHistogramViewItem,
            this.menuItem3,
            this.centerViewItem});
            this.viewItem.MergeOrder = 1;
            this.viewItem.Text = "&View";
            this.viewItem.Visible = false;
            this.viewItem.Popup += new System.EventHandler(this.viewItem_Popup);
            // 
            // mainBarViewItem
            // 
            this.mainBarViewItem.Index = 0;
            this.mainBarViewItem.Text = "m";
            this.mainBarViewItem.Click += new System.EventHandler(this.mainBarViewItem_Click);
            // 
            // imageBarViewItem
            // 
            this.imageBarViewItem.Index = 1;
            this.imageBarViewItem.Text = "Image tool bar";
            this.imageBarViewItem.Click += new System.EventHandler(this.imageBarViewItem_Click);
            // 
            // menuItem7
            // 
            this.menuItem7.Index = 2;
            this.menuItem7.Text = "-";
            // 
            // histogramViewItem
            // 
            this.histogramViewItem.Index = 3;
            this.histogramViewItem.Shortcut = System.Windows.Forms.Shortcut.CtrlH;
            this.histogramViewItem.Text = "&Histogram";
            this.histogramViewItem.Click += new System.EventHandler(this.histogramViewItem_Click);
            // 
            // redHistogramViewItem
            // 
            this.redHistogramViewItem.Index = 4;
            this.redHistogramViewItem.Shortcut = System.Windows.Forms.Shortcut.Ctrl1;
            this.redHistogramViewItem.Text = "R";
            this.redHistogramViewItem.Visible = false;
            this.redHistogramViewItem.Click += new System.EventHandler(this.redHistogramViewItem_Click);
            // 
            // greenHistogramViewItem
            // 
            this.greenHistogramViewItem.Index = 5;
            this.greenHistogramViewItem.Shortcut = System.Windows.Forms.Shortcut.Ctrl2;
            this.greenHistogramViewItem.Text = "G";
            this.greenHistogramViewItem.Visible = false;
            this.greenHistogramViewItem.Click += new System.EventHandler(this.greenHistogramViewItem_Click);
            // 
            // blueHistogramViewItem
            // 
            this.blueHistogramViewItem.Index = 6;
            this.blueHistogramViewItem.Shortcut = System.Windows.Forms.Shortcut.Ctrl3;
            this.blueHistogramViewItem.Text = "B";
            this.blueHistogramViewItem.Visible = false;
            this.blueHistogramViewItem.Click += new System.EventHandler(this.blueHistogramViewItem_Click);
            // 
            // menuItem3
            // 
            this.menuItem3.Index = 7;
            this.menuItem3.Text = "-";
            // 
            // centerViewItem
            // 
            this.centerViewItem.Index = 8;
            this.centerViewItem.Shortcut = System.Windows.Forms.Shortcut.F9;
            this.centerViewItem.Text = "&Center";
            this.centerViewItem.Click += new System.EventHandler(this.centerViewItem_Click);
            // 
            // optionsItem
            // 
            this.optionsItem.Index = 2;
            this.optionsItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.openInNewOptionsItem,
            this.rememberOptionsItem});
            this.optionsItem.MergeOrder = 2;
            this.optionsItem.Text = "&Options";
            this.optionsItem.Visible = false;
            this.optionsItem.Popup += new System.EventHandler(this.optionsItem_Popup);
            // 
            // openInNewOptionsItem
            // 
            this.openInNewOptionsItem.Index = 0;
            this.openInNewOptionsItem.Text = "Open in &new document on change";
            this.openInNewOptionsItem.Click += new System.EventHandler(this.openInNewOptionsItem_Click);
            // 
            // rememberOptionsItem
            // 
            this.rememberOptionsItem.Index = 1;
            this.rememberOptionsItem.Text = "&Remember on change";
            this.rememberOptionsItem.Click += new System.EventHandler(this.rememberOptionsItem_Click);
            // 
            // windowItem
            // 
            this.windowItem.Index = 3;
            this.windowItem.MdiList = true;
            this.windowItem.MergeOrder = 3;
            this.windowItem.Text = "&Window";
            this.windowItem.Visible = false;
            // 
            // statusBar
            // 
            this.statusBar.Dock = System.Windows.Forms.DockStyle.Top;
            this.statusBar.ImeMode = System.Windows.Forms.ImeMode.NoControl;
            this.statusBar.Location = new System.Drawing.Point(0, 0);
            this.statusBar.Name = "statusBar";
            this.statusBar.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
            this.zoomPanel,
            this.sizePanel,
            this.selectionPanel,
            this.colorPanel,
            this.hslPanel,
            this.infoPanel});
            this.statusBar.ShowPanels = true;
            this.statusBar.Size = new System.Drawing.Size(600, 22);
            this.statusBar.TabIndex = 1;
            this.statusBar.PanelClick += new System.Windows.Forms.StatusBarPanelClickEventHandler(this.statusBar_PanelClick);
            // 
            // zoomPanel
            // 
            this.zoomPanel.Name = "zoomPanel";
            this.zoomPanel.ToolTipText = "Zoom coefficient";
            this.zoomPanel.Width = 40;
            // 
            // sizePanel
            // 
            this.sizePanel.Name = "sizePanel";
            this.sizePanel.ToolTipText = "Image size";
            this.sizePanel.Width = 80;
            // 
            // selectionPanel
            // 
            this.selectionPanel.Name = "selectionPanel";
            this.selectionPanel.ToolTipText = "Current point and selection size";
            // 
            // colorPanel
            // 
            this.colorPanel.Name = "colorPanel";
            this.colorPanel.ToolTipText = "Current color";
            this.colorPanel.Width = 170;
            // 
            // hslPanel
            // 
            this.hslPanel.Name = "hslPanel";
            this.hslPanel.Width = 160;
            // 
            // infoPanel
            // 
            this.infoPanel.Name = "infoPanel";
            this.infoPanel.Width = 20;
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.dockManager);
            this.panel1.Controls.Add(this.statusBar);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(600, 358);
            this.panel1.TabIndex = 2;
            // 
            // dockManager
            // 
            this.dockManager.ActiveAutoHideContent = null;
            this.dockManager.AutoScroll = true;
            this.dockManager.BackColor = System.Drawing.Color.Black;
            this.dockManager.Controls.Add(this.mainToolBar);
            this.dockManager.Controls.Add(this.imageToolBar);
            this.dockManager.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dockManager.DockBottomPortion = 0.01;
            this.dockManager.DockLeftPortion = 0.01;
            this.dockManager.DockRightPortion = 0.01;
            this.dockManager.DockTopPortion = 0.01;
            this.dockManager.Font = new System.Drawing.Font("Tahoma", 8.25F);
            this.dockManager.ForeColor = System.Drawing.Color.Peru;
            this.dockManager.Location = new System.Drawing.Point(0, 22);
            this.dockManager.Margin = new System.Windows.Forms.Padding(0);
            this.dockManager.Name = "dockManager";
            this.dockManager.Size = new System.Drawing.Size(600, 336);
            this.dockManager.TabIndex = 2;
            this.dockManager.Paint += new System.Windows.Forms.PaintEventHandler(this.dockManager_Paint);
            this.dockManager.ActiveDocumentChanged += new System.EventHandler(this.dockManager_ActiveDocumentChanged);
            // 
            // mainToolBar
            // 
            this.mainToolBar.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
            this.mainToolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
            this.openButton,
            this.saveButton,
            this.sep1,
            this.copyButton,
            this.pasteButton,
            this.toolBarButton9,
            this.histogramButton});
            this.mainToolBar.Dock = System.Windows.Forms.DockStyle.None;
            this.mainToolBar.DropDownArrows = true;
            this.mainToolBar.ImageList = this.imageList;
            this.mainToolBar.ImeMode = System.Windows.Forms.ImeMode.NoControl;
            this.mainToolBar.Location = new System.Drawing.Point(256, 32);
            this.mainToolBar.Name = "mainToolBar";
            this.mainToolBar.ShowToolTips = true;
            this.mainToolBar.Size = new System.Drawing.Size(24, 160);
            this.mainToolBar.TabIndex = 2;
            this.mainToolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.mainToolBar_ButtonClick);
            // 
            // openButton
            // 
            this.openButton.ImageIndex = 0;
            this.openButton.Name = "openButton";
            this.openButton.ToolTipText = "Open an image ";
            // 
            // saveButton
            // 
            this.saveButton.ImageIndex = 1;
            this.saveButton.Name = "saveButton";
            this.saveButton.ToolTipText = "Save";
            // 
            // sep1
            // 
            this.sep1.Name = "sep1";
            this.sep1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
            // 
            // copyButton
            // 
            this.copyButton.ImageIndex = 2;
            this.copyButton.Name = "copyButton";
            this.copyButton.ToolTipText = "Copy to clipboard";
            // 
            // pasteButton
            // 
            this.pasteButton.ImageIndex = 3;
            this.pasteButton.Name = "pasteButton";
            this.pasteButton.ToolTipText = "Paste from clipboard";
            // 
            // toolBarButton9
            // 
            this.toolBarButton9.Name = "toolBarButton9";
            this.toolBarButton9.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
            // 
            // histogramButton
            // 
            this.histogramButton.ImageIndex = 4;
            this.histogramButton.Name = "histogramButton";
            this.histogramButton.ToolTipText = "Show histogram";
            // 
            // imageList
            // 
            this.imageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList.ImageStream")));
            this.imageList.TransparentColor = System.Drawing.Color.Transparent;
            this.imageList.Images.SetKeyName(0, "");
            this.imageList.Images.SetKeyName(1, "");
            this.imageList.Images.SetKeyName(2, "");
            this.imageList.Images.SetKeyName(3, "");
            this.imageList.Images.SetKeyName(4, "");
            // 
            // imageToolBar
            // 
            this.imageToolBar.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
            this.imageToolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
            this.cloneButton,
            this.toolBarButton1,
            this.cropButton,
            this.toolBarButton2,
            this.zoomInButton,
            this.zoomOutButton,
            this.toolBarButton3,
            this.fitToScreenButton,
            this.toolBarButton5,
            this.resizeButton,
            this.rotateButton,
            this.toolBarButton7,
            this.levelsButton,
            this.grayscaleButton,
            this.thresholdButton,
            this.toolBarButton6,
            this.morphologyButton,
            this.convolutionButton,
            this.toolBarButton8,
            this.saturationButton,
            this.fourierButton});
            this.imageToolBar.Dock = System.Windows.Forms.DockStyle.None;
            this.imageToolBar.DropDownArrows = true;
            this.imageToolBar.ImageList = this.imageList2;
            this.imageToolBar.ImeMode = System.Windows.Forms.ImeMode.NoControl;
            this.imageToolBar.Location = new System.Drawing.Point(12, 275);
            this.imageToolBar.Name = "imageToolBar";
            this.imageToolBar.ShowToolTips = true;
            this.imageToolBar.Size = new System.Drawing.Size(472, 28);
            this.imageToolBar.TabIndex = 3;
            this.imageToolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.imageToolBar_ButtonClick);
            // 
            // cloneButton
            // 
            this.cloneButton.ImageIndex = 0;
            this.cloneButton.Name = "cloneButton";
            this.cloneButton.ToolTipText = "Clone the image";
            // 
            // toolBarButton1
            // 
            this.toolBarButton1.Name = "toolBarButton1";
            this.toolBarButton1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
            // 
            // cropButton
            // 
            this.cropButton.ImageIndex = 1;
            this.cropButton.Name = "cropButton";
            this.cropButton.ToolTipText = "Crop image";
            // 
            // toolBarButton2
            // 
            this.toolBarButton2.Name = "toolBarButton2";
            this.toolBarButton2.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
            // 
            // zoomInButton
            // 
            this.zoomInButton.ImageIndex = 2;
            this.zoomInButton.Name = "zoomInButton";
            this.zoomInButton.ToolTipText = "Zoom In";
            // 
            // zoomOutButton
            // 
            this.zoomOutButton.ImageIndex = 3;
            this.zoomOutButton.Name = "zoomOutButton";
            this.zoomOutButton.ToolTipText = "Zoom out";
            // 
            // toolBarButton3
            // 
            this.toolBarButton3.ImageIndex = 4;
            this.toolBarButton3.Name = "toolBarButton3";
            this.toolBarButton3.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
            this.toolBarButton3.ToolTipText = "Original size";
            // 
            // fitToScreenButton
            // 
            this.fitToScreenButton.ImageIndex = 5;
            this.fitToScreenButton.Name = "fitToScreenButton";
            this.fitToScreenButton.ToolTipText = "Fit to window size";
            // 
            // toolBarButton5
            // 
            this.toolBarButton5.Name = "toolBarButton5";
            this.toolBarButton5.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
            // 
            // resizeButton
            // 
            this.resizeButton.ImageIndex = 11;
            this.resizeButton.Name = "resizeButton";
            this.resizeButton.ToolTipText = "Resize the image";
            // 
            // rotateButton
            // 
            this.rotateButton.ImageIndex = 12;
            this.rotateButton.Name = "rotateButton";
            this.rotateButton.ToolTipText = "Rotate the image";
            // 
            // toolBarButton7
            // 
            this.toolBarButton7.Name = "toolBarButton7";
            this.toolBarButton7.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
            // 
            // levelsButton
            // 
            this.levelsButton.ImageIndex = 6;
            this.levelsButton.Name = "levelsButton";
            this.levelsButton.ToolTipText = "Levels correction";
            // 
            // grayscaleButton
            // 
            this.grayscaleButton.ImageIndex = 7;
            this.grayscaleButton.Name = "grayscaleButton";
            this.grayscaleButton.ToolTipText = "Grayscale";
            // 
            // thresholdButton
            // 
            this.thresholdButton.ImageIndex = 8;
            this.thresholdButton.Name = "thresholdButton";
            this.thresholdButton.ToolTipText = "Threshold";
            // 
            // toolBarButton6
            // 
            this.toolBarButton6.Name = "toolBarButton6";
            this.toolBarButton6.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
            // 
            // morphologyButton
            // 
            this.morphologyButton.ImageIndex = 9;
            this.morphologyButton.Name = "morphologyButton";
            this.morphologyButton.ToolTipText = "Custom morphology operator";
            // 
            // convolutionButton
            // 
            this.convolutionButton.ImageIndex = 10;
            this.convolutionButton.Name = "convolutionButton";
            this.convolutionButton.ToolTipText = "Custom convolution operator";
            // 
            // toolBarButton8
            // 
            this.toolBarButton8.Name = "toolBarButton8";
            this.toolBarButton8.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
            // 
            // saturationButton
            // 
            this.saturationButton.ImageIndex = 13;
            this.saturationButton.Name = "saturationButton";
            this.saturationButton.ToolTipText = "Saturation (HSL)";
            // 
            // fourierButton
            // 
            this.fourierButton.ImageIndex = 14;
            this.fourierButton.Name = "fourierButton";
            this.fourierButton.ToolTipText = "Fourier Transformation";
            // 
            // imageList2
            // 
            this.imageList2.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList2.ImageStream")));
            this.imageList2.TransparentColor = System.Drawing.Color.Transparent;
            this.imageList2.Images.SetKeyName(0, "");
            this.imageList2.Images.SetKeyName(1, "");
            this.imageList2.Images.SetKeyName(2, "");
            this.imageList2.Images.SetKeyName(3, "");
            this.imageList2.Images.SetKeyName(4, "");
            this.imageList2.Images.SetKeyName(5, "");
            this.imageList2.Images.SetKeyName(6, "");
            this.imageList2.Images.SetKeyName(7, "");
            this.imageList2.Images.SetKeyName(8, "");
            this.imageList2.Images.SetKeyName(9, "");
            this.imageList2.Images.SetKeyName(10, "");
            this.imageList2.Images.SetKeyName(11, "");
            this.imageList2.Images.SetKeyName(12, "");
            this.imageList2.Images.SetKeyName(13, "");
            this.imageList2.Images.SetKeyName(14, "");
            // 
            // ofd
            // 
            this.ofd.Filter = "Image files (*.jpg,*.png,*.tif,*.bmp,*.gif)|*.jpg;*.png;*.tif;*.bmp;*.gif|JPG fil" +
                "es (*.jpg)|*.jpg|PNG files (*.png)|*.png|TIF files (*.tif)|*.tif|BMP files (*.bm" +
                "p)|*.bmp|GIF files (*.gif)|*.gif";
            this.ofd.Title = "Open image";
            // 
            // sfd
            // 
            this.sfd.Filter = "JPG files (*.jpg)|*.jpg|BMP files (*.bmp)|*.bmp";
            this.sfd.Title = "Save image";
            // 
            // printDocument
            // 
            this.printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument_PrintPage);
            // 
            // printPreviewDialog
            // 
            this.printPreviewDialog.AutoScrollMargin = new System.Drawing.Size(0, 0);
            this.printPreviewDialog.AutoScrollMinSize = new System.Drawing.Size(0, 0);
            this.printPreviewDialog.ClientSize = new System.Drawing.Size(400, 300);
            this.printPreviewDialog.Enabled = true;
            this.printPreviewDialog.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog.Icon")));
            this.printPreviewDialog.Name = "printPreviewDialog";
            this.printPreviewDialog.Visible = false;
            // 
            // MainForm
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.BackColor = System.Drawing.Color.Black;
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.ClientSize = new System.Drawing.Size(600, 358);
            this.Controls.Add(this.panel1);
            this.Font = new System.Drawing.Font("Tahoma", 8.25F);
            this.ForeColor = System.Drawing.Color.Peru;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.KeyPreview = true;
            this.MaximizeBox = false;
            this.Menu = this.mainMenu;
            this.MinimizeBox = false;
            this.Name = "MainForm";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.Load += new System.EventHandler(this.MainForm_Load);
            this.Closing += new System.ComponentModel.CancelEventHandler(this.MainForm_Closing);
            ((System.ComponentModel.ISupportInitialize)(this.zoomPanel)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.sizePanel)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.selectionPanel)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.colorPanel)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.hslPanel)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.infoPanel)).EndInit();
            this.panel1.ResumeLayout(false);
            this.dockManager.ResumeLayout(false);
            this.dockManager.PerformLayout();
            this.ResumeLayout(false);

    		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}

		#region IDocumentsHost implementation

		// Create new document on change on existent or modify it
		public bool CreateNewDocumentOnChange
		{
			get { return config.openInNewDoc; }
		}

		// Remember or not an image, so we can back one step
		public bool RememberOnChange
		{
			get { return config.rememberOnChange; }
		}

		// Create new document
		public bool NewDocument(Bitmap image)
		{
			unnamedNumber++;

			ImageDoc imgDoc = new ImageDoc(image, (IDocumentsHost) this);

			imgDoc.Text = "Image " + unnamedNumber.ToString();
			imgDoc.Show(dockManager);
			imgDoc.Focus();

			// set events
			SetupDocumentEvents(imgDoc);

			return true;
		}

		// Create new document for ComplexImage
		public bool NewDocument(ComplexImage image)
		{
			unnamedNumber++;

			FourierDoc imgDoc = new FourierDoc(image, (IDocumentsHost) this);

			imgDoc.Text = "Image " + unnamedNumber.ToString();
			imgDoc.Show(dockManager);
			imgDoc.Focus();

			return true;
		}

        public Bitmap CurrentImage( Boolean ARGB)
        {
                        Content	doc = dockManager.ActiveDocument;

                        if ((doc != null) && (doc is ImageDoc))
                        {
                            if (ARGB)
                            {
                                Bitmap img;
                                img = new Bitmap(((ImageDoc)doc).image.Width,((ImageDoc)doc).image.Height,PixelFormat.Format32bppArgb);
                                for (int x = 0; x < img.Width; x++)
                                    for (int y = 0; y < img.Height; y++)
                                        img.SetPixel(x,y,Color.FromArgb(255,((ImageDoc)doc).image.GetPixel(x, y).R, ((ImageDoc)doc).image.GetPixel(x, y).G, ((ImageDoc)doc).image.GetPixel(x, y).B));
                                return img;
                                }
                            else
                                return ((ImageDoc)doc).image;
                        }
                        else
                        {
                            return null;
                        }
        }

		// Get an image with specified dimension and pixel format
		public Bitmap GetImage(object sender, String text, Size size, PixelFormat format)
		{
			ArrayList	names = new ArrayList();
			ArrayList	images = new ArrayList();

			foreach (Content doc in dockManager.Documents)
			{
				if ((doc != sender) && (doc is ImageDoc))
				{
					Bitmap img = ((ImageDoc) doc).Image;

					// check pixel format, width and height
					if ((img.PixelFormat == format) &&
						((size.Width == -1) ||
						((img.Width == size.Width) && (img.Height == size.Height))))
					{
						names.Add(doc.Text);
						images.Add(img);
					}
				}
			}

			SelectImageForm form = new SelectImageForm();

			form.Description = text;
			form.ImageNames = names;

			// allow user to select an image
			if ((form.ShowDialog() == DialogResult.OK) && (form.SelectedItem != -1))
			{
				return (Bitmap) images[form.SelectedItem];
			}

			return null;
		}

		#endregion

        // On form closing
		private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			// close all files
			foreach (Content c in dockManager.Documents)
				c.Close();

			// save configuration
			//config.mainWindowLocation = this.Location;
			//config.mainWindowSize = this.Size;
			//config.Save(configFile);
			// save dock manager configuration
			//dockManager.SaveAsXml(dockManagerConfigFile);
		}

		// On form load
		private void MainForm_Load(object sender, System.EventArgs e)
		{
			// load configuration
		//	if (config.Load(configFile))
	//		{
	//			this.Location = config.mainWindowLocation;
	//			this.Size = config.mainWindowSize;
	//		}

			//try
			//{
			//	// load dock manager configuration
			//	if (File.Exists(dockManagerConfigFile))
		//		dockManager.LoadFromXml(dockManagerConfigFile, new GetContentCallback(GetContentFromPersistString));
		//	}
		//	catch (Exception)
		//	{
		//	}

			// show histogram
			ShowHistogram(config.histogramVisible);
		}

		// Callback for loading Dock Manager
		private Content GetContentFromPersistString(string persistString)
		{
			if (persistString == typeof(HistogramWindow).ToString())
				return histogramWin;

			return null;
		}

		// Main tool bar clicked
		private void mainToolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			switch (e.Button.ImageIndex)
			{
				case 0:		// open an image
					OpenFile();
					break;
				case 1:		// save file
					SaveFile();
					break;
				case 2:		// copy
					CopyToClipboard();
					break;
				case 3:		// paste
					PasteFromClipboard();
					break;
				case 4:		// show histogram window
					ShowHistogram(!config.histogramVisible);
					break;
			}
		}

		// active document changed
		private void dockManager_ActiveDocumentChanged(object sender, System.EventArgs e)
		{
			Content		doc = dockManager.ActiveDocument;
			ImageDoc	imgDoc = (doc is ImageDoc) ? (ImageDoc) doc : null;

			UpdateHistogram(imgDoc);
			UpdateZoomStatus(imgDoc);

			UpdateSizeStatus(doc);
		}

		// on File item popum - set state ot child menu items
		private void fileItem_Popup(object sender, System.EventArgs e)
		{
			Content	doc = dockManager.ActiveDocument;
			bool	docOpened = (doc != null);

			closeFileItem.Enabled = docOpened;
			closeAllFileItem.Enabled = (dockManager.Documents.Length > 0);
			reloadFileItem.Enabled = ((docOpened) && (doc is ImageDoc) && (((ImageDoc) doc).FileName != null));

			saveFileItem.Enabled = docOpened;
			copyFileItem.Enabled = docOpened;
			pasteFileItem.Enabled = (Clipboard.GetDataObject().GetDataPresent(DataFormats.Bitmap));

			printFileItem.Enabled = docOpened;
			printPreviewFileItem.Enabled = docOpened;
		}

		// Exit application
		private void exitFileItem_Click(object sender, System.EventArgs e)
		{
			Close();
		}

		// Setup events
		private void SetupDocumentEvents(ImageDoc doc)
		{
			doc.DocumentChanged += new System.EventHandler(this.document_DocumentChanged);
			doc.ZoomChanged += new System.EventHandler(this.document_ZoomChanged);
            doc.slice += new System.EventHandler(this.document_setslice);
            doc.BaseDatasetChanged += new System.EventHandler(this.BaseDataSet_Changed);
			doc.MouseImagePosition += new ImageDoc.SelectionEventHandler(this.document_MouseImagePosition);
            doc.MouseClickPosition += new ImageDoc.SelectionEventHandler(this.document_MouseClickPosition);

			doc.SelectionChanged += new ImageDoc.SelectionEventHandler(this.document_SelectionChanged);
            doc.ScrollChanged += new System.EventHandler(this.ScrollChanged);
            doc.ShowSubjectInfo += new System.EventHandler(this.ShowSubjectInfo);
            doc.DelThis += new System.EventHandler(this.DelThis);
            doc.DelBefore += new System.EventHandler(this.DelBefore);
            doc.DelAfter += new System.EventHandler(this.DelAfter);
		}
        public event EventHandler SubjectInfo,SliceDeleteThis,SliceDeleteBefore,SliceDeleteAfter;
        private void DelThis(object sender, EventArgs e)
        {
            if (MessageBox.Show("This operation might compromise your FOV size. Press OK to continue.", "Warning", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            { return ; }
            SliceDeleteThis(sender, e);
        }
        private void DelBefore(object sender, EventArgs e)
        {
            if (MessageBox.Show("This operation might compromise your FOV size. Press OK to continue.", "Warning", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            { return ; }
            
            SliceDeleteBefore(sender, e);
        }
        private void DelAfter(object sender, EventArgs e)
        {
            if (MessageBox.Show("This operation might compromise your FOV size. Press OK to continue.", "Warning", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            { return ; }
            
            SliceDeleteAfter(sender, e);
        }
        private void ShowSubjectInfo(object sender, EventArgs e)
        {
            Content doc = dockManager.ActiveDocument;

            if ((doc != null) && (doc is ImageDoc))
                if (((ImageDoc)doc).IsColorMatrixLoaded == true)
                    return;
            SubjectInfo(sender, e);
        }


        public float HScrollBarValue, VScrollBarValue;

        private void ScrollChanged(object sender, EventArgs e)
        {
            Content	doc = dockManager.ActiveDocument;

            if ((doc != null) && (doc is ImageDoc))
            {
               
                    HScrollBarValue = ((ImageDoc)doc).HScrollBarValue;
                    VScrollBarValue = ((ImageDoc)doc).VScrollBarValue;
             
            }
        }

		// Open file
		private void OpenFile()
		{
			if (ofd.ShowDialog() == DialogResult.OK)
			{
				ImageDoc imgDoc = null;
				
				try
				{
					// create image document
					imgDoc = new ImageDoc(ofd.FileName, (IDocumentsHost) this);
					imgDoc.Text = Path.GetFileName(ofd.FileName);

				}
				catch (ApplicationException ex)
				{
					MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}

				if (imgDoc != null)
				{
					imgDoc.Show(dockManager);
					imgDoc.Focus();

					// set events
					SetupDocumentEvents(imgDoc);
				}
			}		
		}

        // Open file
        public void OpenFile(Bitmap g)
        {
            ImageDoc imgDoc = null;

            try
            {
                // create image document
                imgDoc = new ImageDoc(g, (IDocumentsHost)this);
                imgDoc.Text = "Matrix Viewer";

            }
            catch (ApplicationException ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (imgDoc != null)
            {
                imgDoc.Show(dockManager);
                imgDoc.Focus();

                // set events
                SetupDocumentEvents(imgDoc);
            }
        }
        
        public void OpenFile(MatrixDataStructures.SingleVolume Volume, string WindowText, bool matrix, bool matrixD)
        {
            ImageDoc imgDoc = null;

            try
            {
                if ((Volume.Matrix != null)&&(matrix == true))
                {
                    // create image document
                    imgDoc = new ImageDoc(Volume.Matrix.ReScale(Volume.Matrix, 0, 255).data, Volume.Matrix.x, Volume.Matrix.y, Volume.Matrix.z, (IDocumentsHost)this, Volume.FOV, Volume.Center);
                    imgDoc.max = Convert.ToSingle(Volume.Matrix.Maximum());
                    imgDoc.min = Convert.ToSingle(Volume.Matrix.Minimum());

                    imgDoc.Temp3DInt16 = new MatrixDataStructures.Matrix3DInt16(Volume.Matrix.data);

                    imgDoc.Text = WindowText;
                    imgDoc.ScaleConstant = 255.0 / (Volume.Matrix.Maximum() - Volume.Matrix.Minimum());
                    imgDoc.InterceptConstant = Volume.Matrix.Minimum();
                }
                if ((Volume.MatrixD != null)&&(matrixD==true))
                {
                    imgDoc = new ImageDoc((Volume.MatrixD.ReScaleToINT(Volume.MatrixD, 1, 250)).data, Volume.MatrixD.x, Volume.MatrixD.y, Volume.MatrixD.z, (IDocumentsHost)this,Volume.FOV, Volume.Center);
                    imgDoc.Temp3DSingle = Volume.MatrixD;
                    imgDoc.max = Convert.ToSingle(Volume.MatrixD.Maximum());
                    imgDoc.min = Convert.ToSingle(Volume.MatrixD.Minimum());

                    imgDoc.Text = WindowText;
                    imgDoc.ScaleConstant = 255.0 / (Volume.MatrixD.Maximum() - Volume.MatrixD.Minimum());
                    imgDoc.InterceptConstant = Volume.MatrixD.Minimum();
                }
            }
            catch (ApplicationException ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (imgDoc != null)
            {
                imgDoc.Show(dockManager);
                imgDoc.Focus();

                // set events
                SetupDocumentEvents(imgDoc);
            }
        }
        //public void OpenFile(MatrixDataStructures.Matrix3DSingle Volume, string WindowText)
        //{
        //    ImageDoc imgDoc = null;
        //    try
        //    {
        //        // create image document
        //        imgDoc = new ImageDoc((Volume.ReScaleToINT(Volume, 1, 250)).data, Volume.x, Volume.y, Volume.z, (IDocumentsHost)this);
        //        imgDoc.Temp3DSingle = Volume;
            
        //        imgDoc.Text = WindowText;
        //        imgDoc.ScaleConstant = 255.0 / (Volume.Maximum() - Volume.Minimum());
        //        imgDoc.InterceptConstant = Volume.Minimum();

        //    }
        //    catch (ApplicationException ex)
        //    {
        //        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //    }

        //    if (imgDoc != null)
        //    {
        //        imgDoc.Show(dockManager);
        //        imgDoc.Focus();

        //        // set events
        //        SetupDocumentEvents(imgDoc);
        //    }
        //}

        public void OpenFile(MatrixDataStructures.Matrix3DSingle Volume, string WindowText, float scaleslope, float scaleintercept)
        {
            ImageDoc imgDoc = null;
            try
            {
                this.scale = true;
                this.scaleslope = scaleslope;
                this.scaleintercept = scaleintercept;

                // create image document
                imgDoc = new ImageDoc((Volume.ReScaleToINT(Volume, 0, 255)).data, Volume.x, Volume.y, Volume.z, (IDocumentsHost)this, null,null);
                imgDoc.Text = WindowText;
                imgDoc.Temp3DSingle = Volume;
                imgDoc.max = Convert.ToSingle(Volume.Maximum());
                imgDoc.min = Convert.ToSingle(Volume.Minimum());


            }
            catch (ApplicationException ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (imgDoc != null)
            {
                imgDoc.Show(dockManager);
                imgDoc.Focus();

                // set events
                SetupDocumentEvents(imgDoc);
            }
        }

        float scaleslope, scaleintercept;
        bool scale;
        public void OpenFile(MatrixDataStructures.ColorMatrix m, string WindowText)
        {
            ImageDoc imgDoc = null;
            try
            {
                // create image document
                imgDoc = new ImageDoc(m, (IDocumentsHost)this);
                imgDoc.Text = WindowText;
                imgDoc.TempColorMatrix = m;

                imgDoc.max = 255;
                imgDoc.min =0;

            }
            catch (ApplicationException ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (imgDoc != null)
            {
                imgDoc.Show(dockManager);
                imgDoc.Focus();

                // set events
                SetupDocumentEvents(imgDoc);
            }
        }

        public void ChangeCurrentSliceInfo(string type, int slice)
        {
            Content	doc = dockManager.ActiveDocument;

			if ((doc != null) && (doc is ImageDoc))
			{
				try
				{
					((ImageDoc) doc).ChangeCurrentslice(type, slice);

				}
				catch (ApplicationException ex)
				{
					MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
        }

        // Show/hide histogram
		private void ShowHistogram(bool show)
		{
			config.histogramVisible = show;

			histogramViewItem.Checked = show;
			histogramButton.Pushed = show;

            if (this.scaleintercept != 0 )
                scale = !scale;

            //MessageBox.Show("ShowHIstogram " + scale.ToString());
			if (show)
			{
                if (scale)
                    histogramWin.Statistics = ((dockManager.ActiveDocument == null) || !(dockManager.ActiveDocument is ImageDoc)) ? null : new ImageStatistics(((ImageDoc)dockManager.ActiveDocument).Image, true,scaleslope, scaleintercept);
                else
				histogramWin.Statistics = ((dockManager.ActiveDocument == null) || !(dockManager.ActiveDocument is ImageDoc)) ? null : new ImageStatistics(((ImageDoc) dockManager.ActiveDocument).Image,false,0,0);

				histogramWin.Show(dockManager);
			}
			else
			{
				histogramWin.Hide();
			}
		}

		// On "File->Open" item clicked
		private void OpenItem_Click(object sender, System.EventArgs e)
		{
			OpenFile();
		}

		// Reload file
		private void reloadFileItem_Click(object sender, System.EventArgs e)
		{
			Content	doc = dockManager.ActiveDocument;

			if ((doc != null) && (doc is ImageDoc))
			{
				try
				{
					((ImageDoc) doc).Reload();
				}
				catch (ApplicationException ex)
				{
					MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}


        private void BaseDataSet_Changed(object sender, System.EventArgs e)
        {
            //forward the changed dataset through this tunnel
            FilteredBaseDataset(sender,e);
        }

		// Save file
		private void SaveFile()
		{
			Content	doc = dockManager.ActiveDocument;

			if (doc != null)
			{
				// set initial file name
				if ((doc is ImageDoc) && (((ImageDoc) doc).FileName != null))
				{
					sfd.FileName = Path.GetFileName(((ImageDoc) doc).FileName);
				}
				else
				{
					sfd.FileName = doc.Text + ".jpg";
				}

				sfd.FilterIndex = 0;

				// show dialog
				if (sfd.ShowDialog(this) == DialogResult.OK)
				{
					ImageFormat format = ImageFormat.Jpeg;

					// resolve file format
					switch (Path.GetExtension(sfd.FileName))
					{
						case ".jpg":
							format = ImageFormat.Jpeg;
							break;
						case ".bmp":
							format = ImageFormat.Bmp;
							break;
						default:
							MessageBox.Show(this, "Unsupported image format was specified", "Error",
								MessageBoxButtons.OK, MessageBoxIcon.Error);
							return;
					}

					// save the image
					try
					{
						if (doc is ImageDoc)
						{
							((ImageDoc) doc).Image.Save(sfd.FileName, format);
						}
						if (doc is FourierDoc)
						{
							((FourierDoc) doc).Image.Save(sfd.FileName, format);
						}
					}
					catch (Exception)
					{
						MessageBox.Show(this, "Failed writing image file", "Error",
							MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
				}
			}
		}
        int lastx, lasty;
        Color lastcolor;
        public void HighlightPointRed(int x, int y, double value,bool  forcevalue)
        {
            Content doc = dockManager.ActiveDocument;
            if (forcevalue)
            {
                if ((doc != null) && (doc is ImageDoc) && ((ImageDoc)doc).Image != null)
                {
                    try
                    {
                        Bitmap b = new Bitmap(((ImageDoc)doc).Image.Width, ((ImageDoc)doc).Image.Height, PixelFormat.Format24bppRgb);
                        for (int i = 0; i < b.Width; i++)
                            for (int j = 0; j < b.Height; j++)
                                b.SetPixel(i, j, ((ImageDoc)doc).Image.GetPixel(i, j));

                        if ((x >= b.Width) || (y >= b.Height) || (x < 0) || (y < 0) || (lastx >= b.Width) || (lasty >= b.Height) || (lastx < 0) || (lasty < 0))
                        {
                            this.selectionPanel.Text = "Boundary Mismatch";
                            this.colorPanel.Text = "";
                            AmIOnSameSlice = false;
                        }
                        else
                        {
                            if (AmIOnSameSlice == false)
                            {
                                lastx = 0;
                                lasty = 0;
                                lastcolor = Color.Black;
                                this.selectionPanel.Text = "SliceType Mismatch";
                                this.colorPanel.Text = "";
                                AmIOnSameSlice = true;
                            }
                            else
                            {
                                //update only when on the same slice
                                b.SetPixel(lastx, lasty, lastcolor); //restore
                                lastcolor = new Color();
                                lastcolor = (b.GetPixel(x, y));
                                if (((ImageDoc)doc).HotTracking == true)
                                { b.SetPixel(x, y, (Color.FromArgb(255, 0, 0))); }
                                lastx = x;
                                lasty = y;
                                // Release current image
                                ((ImageDoc)doc).image.Dispose();
                                // set document image to just loaded
                                ((ImageDoc)doc).image = b;
                                ((ImageDoc)doc).UpdateNewImage();

                                this.selectionPanel.Text = "(" + x.ToString() + ", " + y.ToString() + ")";
                                this.colorPanel.Text = "Value: " + value.ToString();
                            }
                        }
                    }

                    catch (ApplicationException ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        public void HighlightFibers(int x, int y, double value, bool forcevalue)
        {
            Content doc = dockManager.ActiveDocument;
            if (forcevalue)
            {
                if ((doc != null) && (doc is ImageDoc) && ((ImageDoc)doc).Image != null)
                {
                    try
                    {
                        Bitmap b = new Bitmap(((ImageDoc)doc).Image.Width, ((ImageDoc)doc).Image.Height, PixelFormat.Format24bppRgb);
                        for (int i = 0; i < b.Width; i++)
                            for (int j = 0; j < b.Height; j++)
                                b.SetPixel(i, j, ((ImageDoc)doc).Image.GetPixel(i, j));

                        if ((x >= b.Width) || (y >= b.Height) || (x < 0) || (y < 0) || (lastx >= b.Width) || (lasty >= b.Height) || (lastx < 0) || (lasty < 0))
                        {
                            this.selectionPanel.Text = "Boundary Mismatch";
                            this.colorPanel.Text = "";
                            AmIOnSameSlice = false;
                        }
                        else
                        {
                                if (((ImageDoc)doc).HotTracking == true)
                                { b.SetPixel(x, y, (Color.FromArgb(255, 0, 0))); }
                                // Release current image
                                ((ImageDoc)doc).image.Dispose();
                                // set document image to just loaded
                                ((ImageDoc)doc).image = b;
                                ((ImageDoc)doc).UpdateNewImage();

                                this.selectionPanel.Text = "(" + x.ToString() + ", " + y.ToString() + ")";
                                this.colorPanel.Text = "Value: " + value.ToString();
                           
                        }
                    }

                    catch (ApplicationException ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

		// On "File->Save" - save the file
		private void saveFileItem_Click(object sender, System.EventArgs e)
		{
			SaveFile();
		}

		// Copy image to clipboard
		private void CopyToClipboard()
		{
			Content	doc = dockManager.ActiveDocument;

			if (doc != null)
			{
				if (doc is ImageDoc)
				{
					Clipboard.SetDataObject(((ImageDoc) doc).Image, true);
				}
				if (doc is FourierDoc)
				{
					Clipboard.SetDataObject(((FourierDoc) doc).Image, true);
				}
			}
		}

		// On "File->Copy" - copy image to clipboard
		private void copyFileItem_Click(object sender, System.EventArgs e)
		{
			CopyToClipboard();
		}

		// Paste image from clipboard
		private void PasteFromClipboard()
		{
			if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Bitmap))
			{
				ImageDoc imgDoc = new ImageDoc((Bitmap) Clipboard.GetDataObject().GetData(DataFormats.Bitmap), (IDocumentsHost) this);

				imgDoc.Text = "Image " + unnamedNumber.ToString();
				imgDoc.Show(dockManager);
				imgDoc.Focus();

				// set events
				SetupDocumentEvents(imgDoc);
			}
		}

		// On "File->Paste" - paste image from clipboard
		private void pasteFileItem_Click(object sender, System.EventArgs e)
		{
			PasteFromClipboard();
		}

		// Close file
		private void closeFileItem_Click(object sender, System.EventArgs e)
		{
			if (dockManager.ActiveDocument != null)
				dockManager.ActiveDocument.Close();
		}

		// Close all files
		private void closeAllFileItem_Click(object sender, System.EventArgs e)
		{
			foreach (Content c in dockManager.Documents)
				c.Close();
		}

		// Page setup
		private void pageSetupFileItem_Click(object sender, System.EventArgs e)
		{
			try
			{
				pageSetupDialog.Document = printDocument;
				pageSetupDialog.ShowDialog();
			}
			catch (InvalidPrinterException)
			{
				MessageBox.Show(this, "Failed accessing printer device", "Error",
					MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		// Print image
		private void printFileItem_Click(object sender, System.EventArgs e)
		{
			if (dockManager.ActiveDocument != null)
			{
				try
				{
					printDialog.Document = printDocument;
					if (printDialog.ShowDialog() == DialogResult.OK)
					{
						printDocument.Print();
					}
				}
				catch (InvalidPrinterException)
				{
					MessageBox.Show(this, "Failed accessing printer device", "Error",
						MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}

		// Print preview
		private void printPreviewFileItem_Click(object sender, System.EventArgs e)
		{
			if (dockManager.ActiveDocument != null)
			{
				try
				{
					printPreviewDialog.Document = printDocument;
					printPreviewDialog.ShowDialog();
				}
				catch (InvalidPrinterException)
				{
					MessageBox.Show(this, "Failed accessing printer device", "Error",
						MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}		
		}

		// On "Options" popup
		private void optionsItem_Popup(object sender, System.EventArgs e)
		{
			this.openInNewOptionsItem.Checked = config.openInNewDoc;
			this.rememberOptionsItem.Checked = config.rememberOnChange;
		}

		// On "Options->Open in new Document" click
		private void openInNewOptionsItem_Click(object sender, System.EventArgs e)
		{
			config.openInNewDoc = !config.openInNewDoc;
		}

		// On "Options->Remember on change" click
		private void rememberOptionsItem_Click(object sender, System.EventArgs e)
		{
			config.rememberOnChange = !config.rememberOnChange;
		}

		// On "View" popup
		private void viewItem_Popup(object sender, System.EventArgs e)
		{
			centerViewItem.Enabled = ((dockManager.ActiveDocument != null) && (dockManager.ActiveDocument is ImageDoc));

			ToolBarDockHolder holder;
			// Main tool bar
			holder = toolBarManager.GetHolder(mainToolBar);
			mainBarViewItem.Checked = holder.Visible;
			// Image tool bar
			holder = toolBarManager.GetHolder(imageToolBar);
			imageBarViewItem.Checked = holder.Visible;
		}

		// On "View->Histogram" - show histogram
		private void histogramViewItem_Click(object sender, System.EventArgs e)
		{
			ShowHistogram(!config.histogramVisible);
		}

		// On "View->Center" - center image
		private void centerViewItem_Click(object sender, System.EventArgs e)
		{
			Content	doc = dockManager.ActiveDocument;

			if ((doc != null) && (doc is ImageDoc))
				((ImageDoc) doc).Center();
		}

		// Switch histogram to red channel
		private void redHistogramViewItem_Click(object sender, System.EventArgs e)
		{
			if (histogramWin.Visible)
				histogramWin.SwitchChannel(0);
		}

		// Switch histogram to green channel
		private void greenHistogramViewItem_Click(object sender, System.EventArgs e)
		{
			if (histogramWin.Visible)
				histogramWin.SwitchChannel(1);
		}

		// Switch histogram to blue channel
		private void blueHistogramViewItem_Click(object sender, System.EventArgs e)
		{
			if (histogramWin.Visible)
				histogramWin.SwitchChannel(2);
		}

		// On document changed
		private void document_DocumentChanged(object sender, System.EventArgs e)
		{
			UpdateHistogram((ImageDoc) sender);
			UpdateSizeStatus((ImageDoc) sender);
		}

		// On zoom factor changed
        
        public void ChangeCurrentImageZoom(float zoomfactor)
        {
            Content doc = dockManager.ActiveDocument;

            if ((doc != null) && (doc is ImageDoc))
                ((ImageDoc)doc).ChangeZoomManually(zoomfactor);
        }
        
        public int CurrentSliceIndex()
        {
            Content doc = dockManager.ActiveDocument;

            if ((doc != null) && (doc is ImageDoc))
            {
                if (this.CurrentActiveDisplay == DisplayType.Transverse)
                 return ((ImageDoc)doc).tbar.Value;        
                if(this.CurrentActiveDisplay == DisplayType.Sagittal)
                    return ((ImageDoc)doc).sbar.Value;
                if (this.CurrentActiveDisplay == DisplayType.Coronal)
                    return ((ImageDoc)doc).cbar.Value;
            }
            return 0;
        }


  		
		private void document_ZoomChanged(object sender, System.EventArgs e)
		{
			UpdateZoomStatus((ImageDoc) sender);
        }

        public event EventHandler CurrentSlice;
        public enum DisplayType
        {
            Transverse,
            Coronal,
            Sagittal,
        }
        public  DisplayType CurrentActiveDisplay = DisplayType.Coronal;
        public event EventHandler FilteredBaseDataset;
      
        private void document_setslice(object sender, System.EventArgs e)
		{
            Content doc = dockManager.ActiveDocument;

            if ((doc != null) && (doc is ImageDoc))
            //    if (((ImageDoc)doc).IsColorMatrixLoaded==true)
              //      return;
 
            CurrentSlice((string)(sender), EventArgs.Empty);

            string temp = (string)sender;

            AmIOnSameSlice = false;
            lastx = 0;
            lasty = 0;
            if (temp.Substring(0,1) == "t")
            {
                this.CurrentActiveDisplay = DisplayType.Transverse;
            }
            if (temp.Substring(0,1) == "s")
            { 
                this.CurrentActiveDisplay = DisplayType.Sagittal;
                            }
            if (temp.Substring(0,1) == "c")
            {
                this.CurrentActiveDisplay = DisplayType.Coronal;
            }

		}

        public bool AmIOnSameSlice = true;

		// On mouse position over image changed
        public event EventHandler MouseMovedOnImage, MouseClickedOnImage;

		private void document_MouseImagePosition(object sender, SelectionEventArgs e)
		{
            Content doc = dockManager.ActiveDocument;

            if ((doc != null) && (doc is ImageDoc))
            {
                if ((e.Location.X >= 0) && (e.Location.Y >= 0))
                {
                    this.selectionPanel.Text = "(" + e.Location.X.ToString() + ", " + e.Location.Y.ToString() + ")";

                    // get current color
                    Bitmap image = ((ImageDoc)sender).Image;
                    if (image.PixelFormat == PixelFormat.Format24bppRgb)
                    {
                        Color color = image.GetPixel(e.Location.X, e.Location.Y);
                        this.colorPanel.Text = "RGB: " + color.R.ToString() + "; " + color.G.ToString() + "; " + color.B.ToString();
                        this.hslPanel.Text = "HSL: " + ((int)color.GetHue()).ToString() + "; " + color.GetSaturation().ToString("F3") + "; " + color.GetBrightness().ToString("F3");
                    }
                    else if (image.PixelFormat == PixelFormat.Format8bppIndexed)
                    {
                        Color color = image.GetPixel(e.Location.X, e.Location.Y);
                        this.colorPanel.Text = "Gray: " + color.R.ToString();
                        this.hslPanel.Text = "";
                    }
                    if (this.CurrentActiveDisplay == DisplayType.Transverse)
                    {
                        if (((ImageDoc)doc).Temp3DInt16 != null) { this.colorPanel.Text = "Value " + e.Location.X + "," + e.Location.Y + "," + this.CurrentSliceIndex() + ": " + ((ImageDoc)doc).Temp3DInt16[e.Location.X, e.Location.Y, this.CurrentSliceIndex()].ToString(); }
                        if (((ImageDoc)doc).Temp3DSingle != null) { this.colorPanel.Text = "Value " + e.Location.X + "," + e.Location.Y + "," + this.CurrentSliceIndex() + ": " + ((ImageDoc)doc).Temp3DSingle[e.Location.X, e.Location.Y, this.CurrentSliceIndex()].ToString(); }
                        //if (((ImageDoc)doc).TempColorMatrix != null) { this.colorPanel.Text = "Value: " + (((ImageDoc)doc).TempColorMatrix.data[e.Location.X, e.Location.Y, this.CurrentSliceIndex(), 0]).ToString() + ";" + (((ImageDoc)doc).TempColorMatrix.data[e.Location.X, e.Location.Y, this.CurrentSliceIndex(), 1]).ToString() + ";" + (((ImageDoc)doc).TempColorMatrix.data[e.Location.X, e.Location.Y, this.CurrentSliceIndex(), 2]).ToString(); }

                        //if (this.TempColorMatrix != null) { this.colorPanel.Text = "Value: " + this.TempColorMatrix.data[e.Location.X, e.Location.Y, this.CurrentSliceIndex(),0].ToString(); }
                    }
                    if (this.CurrentActiveDisplay == DisplayType.Sagittal)
                    {
                        if (((ImageDoc)doc).Temp3DInt16 != null) { this.colorPanel.Text = "Value " + ((((ImageDoc)doc).Temp3DInt16.x-1)-this.CurrentSliceIndex())+ "," + (((ImageDoc)doc).Temp3DInt16.y - 1 - e.Location.X)+ "," + (((ImageDoc)doc).Temp3DInt16.z - 1 - e.Location.Y )+ ": " + ((ImageDoc)doc).Temp3DInt16[(((ImageDoc)doc).Temp3DInt16.x-1)-this.CurrentSliceIndex(), ((ImageDoc)doc).Temp3DInt16.y - 1 - e.Location.X, ((ImageDoc)doc).Temp3DInt16.z - 1 - e.Location.Y].ToString(); }
                        if (((ImageDoc)doc).Temp3DSingle != null) { this.colorPanel.Text = "Value " + ((((ImageDoc)doc).Temp3DSingle.x - 1) - this.CurrentSliceIndex()) + "," + (((ImageDoc)doc).Temp3DSingle.y - 1 - e.Location.X) + "," + (((ImageDoc)doc).Temp3DSingle.z - 1 - e.Location.Y) + ": " + ((ImageDoc)doc).Temp3DSingle[this.CurrentSliceIndex() - (((ImageDoc)doc).Temp3DSingle.x - 1), ((ImageDoc)doc).Temp3DSingle.y - 1 - e.Location.X, ((ImageDoc)doc).Temp3DSingle.z - 1 - e.Location.Y].ToString(); }
                        //if (this.TempColorMatrix != null) { this.colorPanel.Text = "Value: " + this.TempColorMatrix.data[e.Location.X, this.CurrentSliceIndex(), e.Location.Y,0].ToString(); }
                    }
                    if (this.CurrentActiveDisplay == DisplayType.Coronal)
                    {
                        if (((ImageDoc)doc).Temp3DInt16 != null) { this.colorPanel.Text = "Value " + e.Location.X+","+ this.CurrentSliceIndex()+","+( ((ImageDoc)doc).Temp3DInt16.z - 1 - e.Location.Y)+": " + ((ImageDoc)doc).Temp3DInt16[e.Location.X, this.CurrentSliceIndex(), ((ImageDoc)doc).Temp3DInt16.z - 1 - e.Location.Y].ToString(); }
                        if (((ImageDoc)doc).Temp3DSingle != null) { this.colorPanel.Text = "Value " + e.Location.X + "," + this.CurrentSliceIndex() + ","+(((ImageDoc)doc).Temp3DSingle.z - 1 - e.Location.Y) + ": " +  ((ImageDoc)doc).Temp3DSingle[e.Location.X, this.CurrentSliceIndex(), ((ImageDoc)doc).Temp3DSingle.z - 1 - e.Location.Y].ToString(); }
                        //if (this.TempColorMatrix != null) { this.colorPanel.Text = "Value: " + this.TempColorMatrix.data[this.CurrentSliceIndex(), e.Location.X, e.Location.Y,0].ToString(); }
                    }

                    if (e.Location != null)
                        MouseMovedOnImage(e.Location, new FormStateEventArgs(CurrentZoom, this.HScrollBarValue, this.VScrollBarValue));
                }

                else
                {
                    this.selectionPanel.Text = "";
                    this.colorPanel.Text = "";
                    this.hslPanel.Text = "";
                }
            }
          }
        private void document_MouseClickPosition(object sender, SelectionEventArgs e)
        {
            Content doc = dockManager.ActiveDocument;

            if ((doc != null) && (doc is ImageDoc))
            {
                if ((e.Location.X >= 0) && (e.Location.Y >= 0))
                {
                   
                    if (e.Location != null)
                        MouseClickedOnImage(e.Location, new FormStateEventArgs(CurrentZoom, this.HScrollBarValue, this.VScrollBarValue));
                }
            }
        }


        // On selection changed
		private void document_SelectionChanged(object sender, SelectionEventArgs e)
		{
			this.selectionPanel.Text = "(" + e.Location.X.ToString() + ", " + e.Location.Y.ToString() + ") - " + e.Size.Width + " x " + e.Size.Height;
		}

		// Update histogram
		private void UpdateHistogram(ImageDoc imgDoc)
		{
			if (histogramWin.Visible)
				histogramWin.Statistics = (imgDoc == null) ? null : new ImageStatistics(imgDoc.Image,false,0,0);
		}

		// Update size status
		private void UpdateSizeStatus(Content doc)
		{
			if (doc != null)
			{
				int w = 0, h = 0;

				if (doc is ImageDoc)
				{
					w = ((ImageDoc) doc).ImageWidth;
					h = ((ImageDoc) doc).ImageHeight;
				}
				else if (doc is FourierDoc)
				{
					w = ((FourierDoc) doc).ImageWidth;
					h = ((FourierDoc) doc).ImageHeight;
				}

				sizePanel.Text = w.ToString() + " x " + h.ToString();
			}
			else
			{
				sizePanel.Text = String.Empty;
			}
		}
        public float CurrentZoom;
		// Update zoom status
		private void UpdateZoomStatus(ImageDoc imgDoc)
		{
			if (imgDoc != null)
		    {	
                int zoom = (int)(imgDoc.Zoom * 100);
				zoomPanel.Text = zoom.ToString() + "%";
                CurrentZoom = imgDoc.Zoom;
            }
			else
			{
				zoomPanel.Text = String.Empty;
			}
         }

		// On image toolbar clicked
		private void imageToolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			Content	doc = dockManager.ActiveDocument;

			if (doc != null)
			{
				if (doc is ImageDoc)
				{
					ImageDocCommands[] cmd = new ImageDocCommands[]
					{
						ImageDocCommands.Clone, ImageDocCommands.Crop,
						ImageDocCommands.ZoomIn, ImageDocCommands.ZoomOut,
						ImageDocCommands.ZoomOriginal, ImageDocCommands.FitToSize,
						ImageDocCommands.Levels, ImageDocCommands.Grayscale,
						ImageDocCommands.Threshold, ImageDocCommands.Morphology,
						ImageDocCommands.Convolution, ImageDocCommands.Resize,
						ImageDocCommands.Rotate, ImageDocCommands.Saturation,
						ImageDocCommands.Fourier
					};

					((ImageDoc) doc).ExecuteCommand(cmd[e.Button.ImageIndex]);
				}
			}
		}

		// On "View->Main Tool bar" menu item click
		private void mainBarViewItem_Click(object sender, System.EventArgs e)
		{
			ToolBarDockHolder holder = toolBarManager.GetHolder(mainToolBar);
			toolBarManager.ShowControl(mainToolBar, !holder.Visible);
		}

		// On "View->Image Tool bar" menu item click
		private void imageBarViewItem_Click(object sender, System.EventArgs e)
		{
			ToolBarDockHolder holder = toolBarManager.GetHolder(imageToolBar);
			toolBarManager.ShowControl(imageToolBar, !holder.Visible);
		}

		// Histogram visibility changed
		private void histogram_DockStateChanged(object sender, System.EventArgs e)
		{
			if (histogramWin.DockState == DockState.Hidden)
			{
				// save to config
				config.histogramVisible = false;

				// update menu & tool bar
				histogramViewItem.Checked = false;
				histogramButton.Pushed = false;
			}
		}

		// Print document page
		private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
		{
			Content	doc = dockManager.ActiveDocument;

			if (doc != null)
			{
				Bitmap image = null;

				// get an image to print
				if (doc is ImageDoc)
				{
					image = ((ImageDoc) doc).Image;
				}
				else if (doc is FourierDoc)
				{
					image = ((FourierDoc) doc).Image;
				}

				System.Diagnostics.Debug.WriteLine("X: " + e.MarginBounds.Left + ", Y = " + e.MarginBounds.Top + ", width = " + e.MarginBounds.Width + ", height = " + e.MarginBounds.Height);
				System.Diagnostics.Debug.WriteLine("X: " + e.PageBounds.Left + ", Y = " + e.PageBounds.Top + ", width = " + e.PageBounds.Width + ", height = " + e.PageBounds.Height);
				//e.PageBounds

				int		width = image.Width;
				int		height = image.Height;

				if ((width > e.MarginBounds.Width) || (height > e.MarginBounds.Height))
				{
					float f = Math.Min((float) e.MarginBounds.Width / width, (float) e.MarginBounds.Height / height);

					width = (int)(f * width);
					height = (int)(f * height);
				}

				e.Graphics.DrawImage(image, e.MarginBounds.Left, e.MarginBounds.Top, width, height);
			}
		}

        private void dockManager_Paint(object sender, PaintEventArgs e)
        {
            base.OnPaint(e);
            ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, Color.Black,
                      1, ButtonBorderStyle.Solid, Color.Black, 1,
                      ButtonBorderStyle.Solid, Color.Black, 1, ButtonBorderStyle.Solid,
                      Color.Black, 1, ButtonBorderStyle.Solid);
     
          //  Size si;
          //  si = new Size(Screen.PrimaryScreen.WorkingArea.Size.Width / 4, Screen.PrimaryScreen.WorkingArea.Size.Height / 4);
          //  this.Size = si;
            this.BackColor = Color.Black;
            this.dockManager.BackColor = Color.Black;
            this.dockManager.BorderStyle = BorderStyle.None;
        }

        private void statusBar_PanelClick(object sender, StatusBarPanelClickEventArgs e)
        {
            fileItem.Visible = !fileItem.Visible;
            viewItem.Visible = !viewItem.Visible;
            optionsItem.Visible = !optionsItem.Visible;
            windowItem.Visible = !windowItem.Visible;
        }

        public event ScrollEventHandler ClientScrollEvent;
        private void ClientScrolled(object sender, ScrollEventArgs e)
        {
            ClientScrollEvent(sender, e);
        }

       
	}
